<template>
  <div class="channelsManagement">
    <h3>渠道管理</h3>
    <div class="opration">
      <el-button type="success" icon="el-icon-plus" @click="addData" v-if="permissions_obj.id_113||permissions_obj.id_all">添加</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="channel"
          label="channel"
          width="200"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="url" label="url"></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="备注"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="status_text"
          label="状态"
          width="90"
        ></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="创建时间"
          width="180"
        ></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_20||permissions_obj.id_all">编辑</el-button>
            <el-button size="mini" type="success" @click="handleDelete(scope.$index, scope.row)" v-if="permissions_obj.id_21||permissions_obj.id_all">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //新增修改弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="modifyEditorDialogVisible" width="40%" center>
      <div class="row" v-if="dialogTitle=='修改渠道'">
        <span>id：</span>
        <el-input placeholder="请输入channel" v-model="inputId" clearable disabled></el-input>
      </div>
      <div class="row">
        <span>channel：</span>
        <el-input placeholder="请输入channel" v-model="inputChannel" clearable></el-input>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <span>备注信息：</span>
        <el-input placeholder="请输入备注信息" v-model="inputNote" clearable type="textarea" :rows="3"></el-input>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "channelsManagement",
  data() {
    return {
      tableData: [],
      options: [
        {
          value: 0,
          label: "下架"
        },
        {
          value: 1,
          label: "上架"
        },
       
      ],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      dialogTitle: "修改渠道",
      modifyEditorDialogVisible: false,
      //弹窗
      inputId:"",
      //弹窗
      inputChannel: "",
      //弹窗状态
      inputStatus: "",
      //弹窗备注信息
      inputNote: "",
      initData: {},
      // 权限管理
       permissions:[],
      permissions_obj:{},
    };
  },
  created(){ 
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff=false;
    this.permissions.forEach((item, index) => {
      if(item==-1){
        oneOff=true;
      }else{
        this.permissions_obj['id_'+item]=item;
      }
    });
    if(oneOff){
      this.permissions_obj.id_all=true;
    }else{
      this.permissions_obj.id_all=false;
    };
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
     handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    //新增
    addData() {
      this.inputId="";
      this.inputChannel="";
      this.inputStatus="";
      this.inputNote="";
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "新建渠道";
    },
    //编辑
    handleEdit(index, row) {
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "修改渠道";
      this.inputId=row.id;
      this.inputChannel=row.channel;
      this.inputStatus=row.status;
      this.inputNote=row.remark;

    },
    //删除
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该行数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         this.deleteMethod(row.id);
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //提交修改或者新增
    submitModifyEditor() {
      if(this.dialogTitle =="新建渠道"){
        this.submitAddMethod();
      }else if(this.dialogTitle =="修改渠道"){
        this.submitModifyMethod();
      };
    },
    //取消提交
    cancelModifyEditor() {
      this.modifyEditorDialogVisible = false;
    },
    //获取初始化用户列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            channel: ""
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.channelLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if (item.status == 0) {
            item.status_text = "下架";
          } else if (item.status == 1) {
            item.status_text = "上架";
          }
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //添加接口
    async submitAddMethod() {
      let parames = {
        handshake: "",
        data: {
          channel: this.inputChannel,
          status: this.inputStatus,
          remark: this.inputNote,
        }
      };
      let res = await this.$axios.post(
        this.$api.channelAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //编辑接口
    async submitModifyMethod() {
      let parames = {
        handshake: "",
        data: {
          id:this.inputId,
          channel: this.inputChannel,
          status: this.inputStatus,
          remark: this.inputNote,
        }
      };
      let res = await this.$axios.post(
        this.$api.channelUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          id:id,
        }
      };
      let res = await this.$axios.post(
        this.$api.channelDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>